package com.dpiinc.TechPage.pen.dialog;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.dpiinc.TechPage.R;
import com.dpiinc.TechPage.user.CommonWebviewActivity;
import com.dpiinc.TechPage.user.utils.Constant;

import org.jetbrains.annotations.Nullable;

import java.util.Locale;

/**
 * Created by hu489_000 on 2018-07-23.
 */

public class PrivacyRightsDialog extends BaseFloatDialog {

    public interface OnClickEditListener {
        void onClickOK();
        void onClickCancel();
    }

    private static   OnClickEditListener onClickEditListener;

    private static PrivacyRightsDialog mPrivacyRightsDialog;

    private Context mContext;

    public PrivacyRightsDialog(@Nullable Context context,OnClickEditListener onClickEditListener) {
        super(context);
        this.onClickEditListener = onClickEditListener;
    }



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_privacy_rights);

    }
    public static void ShowDialog(@Nullable final Context context, OnClickEditListener onListener) {
        mPrivacyRightsDialog = new PrivacyRightsDialog(context, onListener);
        mPrivacyRightsDialog.show();

        TextView tv_message1 = (TextView) mPrivacyRightsDialog.findViewById(R.id.tv_message1);
        tv_message1.setText("       " + context.getResources().getString(R.string.privacy_agreement_txt1));

        TextView tv_message2 = mPrivacyRightsDialog.findViewById(R.id.tv_message2);
        String str1 = context.getResources().getString(R.string.privacy_agreement_txt2);
        String str2 = context.getResources().getString(R.string.privacy_agreement_txt3);
        String str3 = context.getResources().getString(R.string.privacy_agreement_txt3_2);
        String str4 = context.getResources().getString(R.string.privacy_agreement_txt3_3);
        SpannableStringBuilder ssb = new SpannableStringBuilder();
        ssb.append("       " + str1 + str2 + str3 + str4
                + context.getResources().getString(R.string.privacy_agreement_txt3_1));
        ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
              //  if ("en".equals(context.getSharedPreferences("app_language", Context.MODE_PRIVATE).getString("lauType", Locale.getDefault().getLanguage()))) {
                    CommonWebviewActivity.webviewEntrance(context, Constant.PROTOCOL_AGREEMENT, "Agreement");
              /*  } else {
                    CommonWebviewActivity.webviewEntrance(context, Constant.APP_AGREEMENT, "用户协议");
                }*/
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                ds.setUnderlineText(false);
            }
        };
        ClickableSpan clickableSpan2 = new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
               // if ("en".equals(context.getSharedPreferences("app_language", Context.MODE_PRIVATE).getString("lauType", Locale.getDefault().getLanguage()))) {
                    CommonWebviewActivity.webviewEntrance(context, Constant.PROTOCOL_AGREEMENT, "Agreement");
             /*   } else {
                    CommonWebviewActivity.webviewEntrance(context, Constant.APP_PROTOCOL, "隐私政策");
                }*/
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                ds.setUnderlineText(false);
            }
        };

        int len = ("       " + str1).length();
        int len2 = ("       " + str1 + str2 + str3).length();
        ssb.setSpan(clickableSpan, len, len + str2.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        ssb.setSpan(clickableSpan2, len2, len2 + str4.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.parseColor("#00BFA5"));//00BFA5
        ForegroundColorSpan foregroundColorSpan2 = new ForegroundColorSpan(Color.parseColor("#00BFA5"));//00BFA5
        ssb.setSpan(foregroundColorSpan, len, len + str2.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        ssb.setSpan(foregroundColorSpan2, len2, len2 + str4.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv_message2.setMovementMethod(LinkMovementMethod.getInstance());
        tv_message2.setText(ssb);

        Button btn_cancel = mPrivacyRightsDialog.findViewById(R.id.btn_cancel);
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismissDialog();
                onClickEditListener.onClickCancel();
            }
        });
        Button btn_ok = mPrivacyRightsDialog.findViewById(R.id.btn_ok);
        btn_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismissDialog();
                onClickEditListener.onClickOK();
            }
        });

    }

    public static void dismissDialog() {
        if (mPrivacyRightsDialog != null && mPrivacyRightsDialog.isShowing()) {
            mPrivacyRightsDialog.dismiss();
        }
    }
}
